package com.lagou.app;

import com.lagou.entity.Phone;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TestQuery {

    public static void main(String[] args) throws SQLException {

        //获取QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        //需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
        String sql = "select * from phone where year(prodate) < ? and price > ?";
        //使用BeanListHandle结果集处理类，将数据封装到Phone里，然后将数据存储在list集合中
        List<Phone> phoneList = queryRunner.query(sql, new BeanListHandler<Phone>(Phone.class), 2019, 2000);

        for (Phone phone : phoneList) {
            System.out.println(phone);
        }

        System.out.println("-------------------------------------------");
        //需求2:  查询所有颜色是白色的手机信息
        String colorsql = "select * from phone where color = ?";
        //将数据存储在list集合中，类型是Phone
        List<Phone> whitephones = queryRunner.query(colorsql, new BeanListHandler<Phone>(Phone.class), "白色");

        for (Phone phone : whitephones) {
            System.out.println(phone);
        }
    }

}
